#!/usr/bin/ruby
# -*- coding: utf-8 -*-
class Commands
  @@commands={}
  def initialize
  end
  def self.get_command(command)
    @@commands[command]
  end
  def self.add_command(name,proc)
    if name.class.to_s!='Symbol' then
      @@log.info "#{name} is not a Symbol object"
      @@log.info name.class
      return
    end
    if proc.class.to_s!='Proc' then
      @@log.info "regist #{name} not is a Proc object"
      return
    end
    @@commands[name]=proc
  end

  def self.do_command(command,args)
    response=""
    proc=Commands.get_command(command.to_sym)
    if proc then
      response+="------------------\n"
      response+=proc.call(args).to_s+"\n"
      response+="*****************\n"
      response+="#{command}<---命令已经执行完毕\n\n"
    else
      response+="*****************\n"
      response+="没有这个#{command}命令\n\n"
    end
    return response
  end

  def self.load_commands
    @@log.info 'clear commands hash'
    @@commands.clear
    dir=Dir.new('commands')
    dir.each {|name|
      if name=~/rb$/ and name !~ /#/ then
        @@log.info "load commands/#{name}"
        load "commands/#{name}"
      end
    }
  end

  def self.commands
    @@commands
  end
end



Commands.load_commands()
